package service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import pojo.User;
import service.UserManager;
@Service(value="umi1")
public class UserManagerImpl implements UserManager {
	private List<User> users = new ArrayList<User>();
	public UserManagerImpl(){
		User  user1 = new User(UUID.randomUUID().toString(), "Tom", true, 2351.2, new Date());
		User  user2 = new User(UUID.randomUUID().toString(), "Alice", false, 5432.2, new Date());
		users.add(user1);
		users.add(user2);
	}
	@Override
	public User addUser(User user) {
		synchronized (user) {
			user.setId(UUID.randomUUID().toString());
			users.add(user);
		}
		return user;
	}

	@Override
	public String deleteUser(String id) {
		for(int i=0;i<users.size();i++){
			if(id.equals(users.get(i).getId())){
				users.remove(i);
			}
		}
		return id;
	}

	@Override
	public User updataUser(User user) {
		for(int i=0;i<users.size();i++){
			if(user.getId().equals(users.get(i).getId())){
				users.get(i).setBirth(user.getBirth());
				users.get(i).setName(user.getName());
				users.get(i).setSal(user.getSal());
				users.get(i).setSex(user.isSex());
			}
		}
		return user;
	}

	@Override
	public List<User> findAll() {
		
		return users;
	}

}
